package com.springboot.controller.web;

import com.springboot.model.DoctorAppointSet;
import com.springboot.model.DoctorInfoForm;
import com.springboot.service.DoctorAppointSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@Controller
public class DoctorSetOrderController {
    @Autowired
    DoctorAppointSetService doctorAppointSetService;

    //设置session
    @CrossOrigin
    @RequestMapping(value = "/websetsessionbyorderdate")
    @ResponseBody String setSessionByOrderDate(@RequestParam("orderDate") String orderDate,
                                               @RequestParam("morningNum") Integer morningNum,
                                               @RequestParam("afternoonNum") Integer afternoonNum,
                                               HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        session.setAttribute("orderDate",orderDate);
        session.setAttribute("morningNum",morningNum);
        session.setAttribute("afternoonNum",afternoonNum);
        System.out.println("成功设置session");
        return "True";
    }

    /**
     * 医生设置可预约时间
     */
    @CrossOrigin
    @RequestMapping(value = "/websetdoctororder")
    public @ResponseBody
    String webSetDoctorOrder(@RequestParam("morningNum") String morningNum,
                            @RequestParam("afternoonNum") String afternoonNum,HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        String tel = (String)session.getAttribute("tel");
        String date = (String)session.getAttribute("orderDate");
        System.out.println("医生设置可预约时间");
        try {
            int morningNumber=Integer.valueOf(morningNum);
            int afternoonNumber=Integer.valueOf(afternoonNum);
            System.out.println(tel+" "+date+" "+ morningNum+" "+afternoonNum);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
            DoctorInfoForm doctor = doctorAppointSetService.findDoctorByTel(tel);
            int doctorId = doctor.getDoctorId();
            System.out.println(doctorId);
            //根据id查找该医生的可预约时间表
            ArrayList<DoctorAppointSet> doctorSets = doctorAppointSetService.findDoctorAppointById(doctorId);
            if(doctorSets.size()==0)
            {
                DoctorAppointSet doctorAppointSet=new DoctorAppointSet();
                doctorAppointSet.setDoctorId(doctorId);
                doctorAppointSet.setBookDate(format.parse(date));
                doctorAppointSet.setMorningNum(morningNumber);
                doctorAppointSet.setAfternoonNum(afternoonNumber);
                doctorAppointSetService.insertDoctorAppoint(doctorAppointSet);
                return "True";
            }
            else
            {
                String []arrayDate=new String[doctorSets.size()+1];
                Boolean isExist=false;
                for (int i = 0; i < doctorSets.size(); i++)
                {
                    DoctorAppointSet doctorAppointSet=doctorSets.get(i);
                    String appointDate = format.format(doctorAppointSet.getBookDate());
                    arrayDate[i]=appointDate;
                }
                for (int i = 0; i < doctorSets.size(); i++)
                {
                    //如果该日期已经填过，则表示修改可预约时间
                    if (arrayDate[i].equals(date))
                    {
                        isExist=true;
                        break;
                    }
                }
                DoctorAppointSet doctorAppointSet=new DoctorAppointSet();
                //如果该日期已经填过，则表示修改可预约时间
                if(isExist)
                {
                    doctorAppointSet.setDoctorId(doctorId);
                    doctorAppointSet.setBookDate(format.parse(date));
                    doctorAppointSet.setMorningNum(morningNumber);
                    doctorAppointSet.setAfternoonNum(afternoonNumber);
                    doctorAppointSetService.updateDoctorSet(doctorAppointSet);
                    return "True";
                }
                //如果该日期没有填过，则表示设置可预约时间
                else
                {
                    doctorAppointSet.setDoctorId(doctorId);
                    doctorAppointSet.setBookDate(format.parse(date));
                    doctorAppointSet.setMorningNum(morningNumber);
                    doctorAppointSet.setAfternoonNum(afternoonNumber);
                    doctorAppointSetService.insertDoctorAppoint(doctorAppointSet);
                    return "True";
                }
            }
        }catch (Exception e)
        {
            return "False";
        }
    }
}
